/**
 * Created by liubin on 2015/09/17.
 */

import {LOGIN, LOGOUT, AUTH, Authorization} from 'actions/authAction';
const { ANONYMOUS, USER } = Authorization;
import { SUCCESS } from 'actions/baseAction';

const initialState = {
    authed: ANONYMOUS,
    logined: false
};

export function authorization(state = 'DEFAULT', action = {type: 'DEFAULT'}) {
    switch (action.type) {
    case LOGIN:
        if (action.status == SUCCESS ) {
            return Object.assign({}, state, {
                authed: USER,
                logined: true
            });
        } else {
            return Object.assign({}, state, {
                authed: ANONYMOUS
            });
        }
        break;
    case LOGOUT:
        return Object.assign({}, state, {
            authed: ANONYMOUS,
            logined: false
        });
    case AUTH:
        if (action.status == SUCCESS) {
            return Object.assign({}, state, {
                authed: USER
            });
        } else {
            return Object.assign({}, state, {
                authed: ANONYMOUS
            });
        }
        break;
    default:
        return state === 'DEFAULT' ? initialState : state;
    }
}